package com.org.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.org.DTO.PageDTO;
import com.org.entity.PlaceDictionarie;
import com.org.mapper.DictionarieMapper;
import com.org.service.DictionarieService;
import com.org.vo.PageResult;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
public class DictionarieServiceImpl extends ServiceImpl<DictionarieMapper, PlaceDictionarie>
        implements DictionarieService {

    private final DictionarieMapper dictionarieMapper;

    /**
     * 查询所有地点字典
     * @param pageDTO
     * @return
     */
    @Override
    public PageResult findAllPlace(PageDTO pageDTO) {
        Page<PlaceDictionarie> page = lambdaQuery()
                .page(new Page<>(pageDTO.getPageNum(), pageDTO.getPageSize()));
        return new PageResult(page.getTotal(), page.getRecords());
    }

    /**
     * 添加地点字典
     * @param place
     */
    @Override
    public void addPlace(PlaceDictionarie place) {
        place.setCreateTime(LocalDateTime.now());
        place.setUpdateTime(LocalDateTime.now());
        dictionarieMapper.insert(place);
    }

    /**
     * 修改地点字典
     * @param place
     */
    @Override
    public void updatePlace(PlaceDictionarie place) {
        LambdaUpdateWrapper<PlaceDictionarie> uw = new LambdaUpdateWrapper<PlaceDictionarie>()
                .set(PlaceDictionarie::getPlace, place.getPlace())
                .set(PlaceDictionarie::getUpdateTime, LocalDateTime.now())
                .eq(PlaceDictionarie::getId, place.getId());
        dictionarieMapper.update(place, uw);
    }

    /**
     * 删除地点字典
     * @param id
     */
    @Override
    public void deletePlace(Long id) {
        //TODO 1、查找该地点下的帖子(失物招领、寻物启事) -- 可实现
        //删除地点字典
        dictionarieMapper.deleteById(id);
    }
}
